package qldc.pm.tiensu.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tiensu.NoSuchDoiTuongTienSuException;
import qldc.pm.tiensu.model.DoiTuongTienSu;
import qldc.pm.tiensu.model.impl.DoiTuongTienSuImpl;
import qldc.pm.tiensu.model.impl.DoiTuongTienSuModelImpl;
import qldc.pm.tiensu.service.persistence.DoiTuongTienSuPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the doi tuong tien su service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see DoiTuongTienSuPersistence
 * @see DoiTuongTienSuUtil
 * @generated
 */
public class DoiTuongTienSuPersistenceImpl extends BasePersistenceImpl<DoiTuongTienSu>
    implements DoiTuongTienSuPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link DoiTuongTienSuUtil} to access the doi tuong tien su persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = DoiTuongTienSuImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienSuImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienSuImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_SODINHDANHCANHAN = new FinderPath(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienSuImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchBySoDinhDanhCaNhan", new String[] { String.class.getName() },
            DoiTuongTienSuModelImpl.SODINHDANHCANHAN_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SODINHDANHCANHAN = new FinderPath(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countBySoDinhDanhCaNhan", new String[] { String.class.getName() });
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1 =
        "doiTuongTienSu.soDinhDanhCaNhan IS NULL";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2 =
        "doiTuongTienSu.soDinhDanhCaNhan = ?";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3 =
        "(doiTuongTienSu.soDinhDanhCaNhan IS NULL OR doiTuongTienSu.soDinhDanhCaNhan = '')";
    private static final String _SQL_SELECT_DOITUONGTIENSU = "SELECT doiTuongTienSu FROM DoiTuongTienSu doiTuongTienSu";
    private static final String _SQL_SELECT_DOITUONGTIENSU_WHERE = "SELECT doiTuongTienSu FROM DoiTuongTienSu doiTuongTienSu WHERE ";
    private static final String _SQL_COUNT_DOITUONGTIENSU = "SELECT COUNT(doiTuongTienSu) FROM DoiTuongTienSu doiTuongTienSu";
    private static final String _SQL_COUNT_DOITUONGTIENSU_WHERE = "SELECT COUNT(doiTuongTienSu) FROM DoiTuongTienSu doiTuongTienSu WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "doiTuongTienSu.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DoiTuongTienSu exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DoiTuongTienSu exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(DoiTuongTienSuPersistenceImpl.class);
    private static DoiTuongTienSu _nullDoiTuongTienSu = new DoiTuongTienSuImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<DoiTuongTienSu> toCacheModel() {
                return _nullDoiTuongTienSuCacheModel;
            }
        };

    private static CacheModel<DoiTuongTienSu> _nullDoiTuongTienSuCacheModel = new CacheModel<DoiTuongTienSu>() {
            @Override
            public DoiTuongTienSu toEntityModel() {
                return _nullDoiTuongTienSu;
            }
        };

    public DoiTuongTienSuPersistenceImpl() {
        setModelClass(DoiTuongTienSu.class);
    }

    /**
     * Returns the doi tuong tien su where soDinhDanhCaNhan = &#63; or throws a {@link qldc.pm.tiensu.NoSuchDoiTuongTienSuException} if it could not be found.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching doi tuong tien su
     * @throws qldc.pm.tiensu.NoSuchDoiTuongTienSuException if a matching doi tuong tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu findBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchDoiTuongTienSuException, SystemException {
        DoiTuongTienSu doiTuongTienSu = fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        if (doiTuongTienSu == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("soDinhDanhCaNhan=");
            msg.append(soDinhDanhCaNhan);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

            throw new NoSuchDoiTuongTienSuException(msg.toString());
        }

        return doiTuongTienSu;
    }

    /**
     * Returns the doi tuong tien su where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching doi tuong tien su, or <code>null</code> if a matching doi tuong tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu fetchBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        return fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan, true);
    }

    /**
     * Returns the doi tuong tien su where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching doi tuong tien su, or <code>null</code> if a matching doi tuong tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu fetchBySoDinhDanhCaNhan(String soDinhDanhCaNhan,
        boolean retrieveFromCache) throws SystemException {
        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs, this);
        }

        if (result instanceof DoiTuongTienSu) {
            DoiTuongTienSu doiTuongTienSu = (DoiTuongTienSu) result;

            if (!Validator.equals(soDinhDanhCaNhan,
                        doiTuongTienSu.getSoDinhDanhCaNhan())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_DOITUONGTIENSU_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                List<DoiTuongTienSu> list = q.list();

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                        finderArgs, list);
                } else {
                    if ((list.size() > 1) && _log.isWarnEnabled()) {
                        _log.warn(
                            "DoiTuongTienSuPersistenceImpl.fetchBySoDinhDanhCaNhan(String, boolean) with parameters (" +
                            StringUtil.merge(finderArgs) +
                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                    }

                    DoiTuongTienSu doiTuongTienSu = list.get(0);

                    result = doiTuongTienSu;

                    cacheResult(doiTuongTienSu);

                    if ((doiTuongTienSu.getSoDinhDanhCaNhan() == null) ||
                            !doiTuongTienSu.getSoDinhDanhCaNhan()
                                               .equals(soDinhDanhCaNhan)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                            finderArgs, doiTuongTienSu);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (DoiTuongTienSu) result;
        }
    }

    /**
     * Removes the doi tuong tien su where soDinhDanhCaNhan = &#63; from the database.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the doi tuong tien su that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu removeBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchDoiTuongTienSuException, SystemException {
        DoiTuongTienSu doiTuongTienSu = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        return remove(doiTuongTienSu);
    }

    /**
     * Returns the number of doi tuong tien sus where soDinhDanhCaNhan = &#63;.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the number of matching doi tuong tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SODINHDANHCANHAN;

        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_DOITUONGTIENSU_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the doi tuong tien su in the entity cache if it is enabled.
     *
     * @param doiTuongTienSu the doi tuong tien su
     */
    @Override
    public void cacheResult(DoiTuongTienSu doiTuongTienSu) {
        EntityCacheUtil.putResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuImpl.class, doiTuongTienSu.getPrimaryKey(),
            doiTuongTienSu);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
            new Object[] { doiTuongTienSu.getSoDinhDanhCaNhan() },
            doiTuongTienSu);

        doiTuongTienSu.resetOriginalValues();
    }

    /**
     * Caches the doi tuong tien sus in the entity cache if it is enabled.
     *
     * @param doiTuongTienSus the doi tuong tien sus
     */
    @Override
    public void cacheResult(List<DoiTuongTienSu> doiTuongTienSus) {
        for (DoiTuongTienSu doiTuongTienSu : doiTuongTienSus) {
            if (EntityCacheUtil.getResult(
                        DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
                        DoiTuongTienSuImpl.class, doiTuongTienSu.getPrimaryKey()) == null) {
                cacheResult(doiTuongTienSu);
            } else {
                doiTuongTienSu.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all doi tuong tien sus.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(DoiTuongTienSuImpl.class.getName());
        }

        EntityCacheUtil.clearCache(DoiTuongTienSuImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the doi tuong tien su.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(DoiTuongTienSu doiTuongTienSu) {
        EntityCacheUtil.removeResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuImpl.class, doiTuongTienSu.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache(doiTuongTienSu);
    }

    @Override
    public void clearCache(List<DoiTuongTienSu> doiTuongTienSus) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (DoiTuongTienSu doiTuongTienSu : doiTuongTienSus) {
            EntityCacheUtil.removeResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
                DoiTuongTienSuImpl.class, doiTuongTienSu.getPrimaryKey());

            clearUniqueFindersCache(doiTuongTienSu);
        }
    }

    protected void cacheUniqueFindersCache(DoiTuongTienSu doiTuongTienSu) {
        if (doiTuongTienSu.isNew()) {
            Object[] args = new Object[] { doiTuongTienSu.getSoDinhDanhCaNhan() };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args, Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args, doiTuongTienSu);
        } else {
            DoiTuongTienSuModelImpl doiTuongTienSuModelImpl = (DoiTuongTienSuModelImpl) doiTuongTienSu;

            if ((doiTuongTienSuModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        doiTuongTienSu.getSoDinhDanhCaNhan()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                    args, Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    args, doiTuongTienSu);
            }
        }
    }

    protected void clearUniqueFindersCache(DoiTuongTienSu doiTuongTienSu) {
        DoiTuongTienSuModelImpl doiTuongTienSuModelImpl = (DoiTuongTienSuModelImpl) doiTuongTienSu;

        Object[] args = new Object[] { doiTuongTienSu.getSoDinhDanhCaNhan() };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN, args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN, args);

        if ((doiTuongTienSuModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
            args = new Object[] {
                    doiTuongTienSuModelImpl.getOriginalSoDinhDanhCaNhan()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args);
        }
    }

    /**
     * Creates a new doi tuong tien su with the primary key. Does not add the doi tuong tien su to the database.
     *
     * @param doiTuongTienSuId the primary key for the new doi tuong tien su
     * @return the new doi tuong tien su
     */
    @Override
    public DoiTuongTienSu create(long doiTuongTienSuId) {
        DoiTuongTienSu doiTuongTienSu = new DoiTuongTienSuImpl();

        doiTuongTienSu.setNew(true);
        doiTuongTienSu.setPrimaryKey(doiTuongTienSuId);

        return doiTuongTienSu;
    }

    /**
     * Removes the doi tuong tien su with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param doiTuongTienSuId the primary key of the doi tuong tien su
     * @return the doi tuong tien su that was removed
     * @throws qldc.pm.tiensu.NoSuchDoiTuongTienSuException if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu remove(long doiTuongTienSuId)
        throws NoSuchDoiTuongTienSuException, SystemException {
        return remove((Serializable) doiTuongTienSuId);
    }

    /**
     * Removes the doi tuong tien su with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the doi tuong tien su
     * @return the doi tuong tien su that was removed
     * @throws qldc.pm.tiensu.NoSuchDoiTuongTienSuException if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu remove(Serializable primaryKey)
        throws NoSuchDoiTuongTienSuException, SystemException {
        Session session = null;

        try {
            session = openSession();

            DoiTuongTienSu doiTuongTienSu = (DoiTuongTienSu) session.get(DoiTuongTienSuImpl.class,
                    primaryKey);

            if (doiTuongTienSu == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchDoiTuongTienSuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(doiTuongTienSu);
        } catch (NoSuchDoiTuongTienSuException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected DoiTuongTienSu removeImpl(DoiTuongTienSu doiTuongTienSu)
        throws SystemException {
        doiTuongTienSu = toUnwrappedModel(doiTuongTienSu);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(doiTuongTienSu)) {
                doiTuongTienSu = (DoiTuongTienSu) session.get(DoiTuongTienSuImpl.class,
                        doiTuongTienSu.getPrimaryKeyObj());
            }

            if (doiTuongTienSu != null) {
                session.delete(doiTuongTienSu);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (doiTuongTienSu != null) {
            clearCache(doiTuongTienSu);
        }

        return doiTuongTienSu;
    }

    @Override
    public DoiTuongTienSu updateImpl(
        qldc.pm.tiensu.model.DoiTuongTienSu doiTuongTienSu)
        throws SystemException {
        doiTuongTienSu = toUnwrappedModel(doiTuongTienSu);

        boolean isNew = doiTuongTienSu.isNew();

        Session session = null;

        try {
            session = openSession();

            if (doiTuongTienSu.isNew()) {
                session.save(doiTuongTienSu);

                doiTuongTienSu.setNew(false);
            } else {
                session.merge(doiTuongTienSu);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !DoiTuongTienSuModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        EntityCacheUtil.putResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienSuImpl.class, doiTuongTienSu.getPrimaryKey(),
            doiTuongTienSu);

        clearUniqueFindersCache(doiTuongTienSu);
        cacheUniqueFindersCache(doiTuongTienSu);

        return doiTuongTienSu;
    }

    protected DoiTuongTienSu toUnwrappedModel(DoiTuongTienSu doiTuongTienSu) {
        if (doiTuongTienSu instanceof DoiTuongTienSuImpl) {
            return doiTuongTienSu;
        }

        DoiTuongTienSuImpl doiTuongTienSuImpl = new DoiTuongTienSuImpl();

        doiTuongTienSuImpl.setNew(doiTuongTienSu.isNew());
        doiTuongTienSuImpl.setPrimaryKey(doiTuongTienSu.getPrimaryKey());

        doiTuongTienSuImpl.setDoiTuongTienSuId(doiTuongTienSu.getDoiTuongTienSuId());
        doiTuongTienSuImpl.setUserId(doiTuongTienSu.getUserId());
        doiTuongTienSuImpl.setUserName(doiTuongTienSu.getUserName());
        doiTuongTienSuImpl.setCreateDate(doiTuongTienSu.getCreateDate());
        doiTuongTienSuImpl.setModifiedDate(doiTuongTienSu.getModifiedDate());
        doiTuongTienSuImpl.setSoDinhDanhCaNhan(doiTuongTienSu.getSoDinhDanhCaNhan());
        doiTuongTienSuImpl.setXoaAnTich(doiTuongTienSu.getXoaAnTich());

        return doiTuongTienSuImpl;
    }

    /**
     * Returns the doi tuong tien su with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the doi tuong tien su
     * @return the doi tuong tien su
     * @throws qldc.pm.tiensu.NoSuchDoiTuongTienSuException if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu findByPrimaryKey(Serializable primaryKey)
        throws NoSuchDoiTuongTienSuException, SystemException {
        DoiTuongTienSu doiTuongTienSu = fetchByPrimaryKey(primaryKey);

        if (doiTuongTienSu == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchDoiTuongTienSuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return doiTuongTienSu;
    }

    /**
     * Returns the doi tuong tien su with the primary key or throws a {@link qldc.pm.tiensu.NoSuchDoiTuongTienSuException} if it could not be found.
     *
     * @param doiTuongTienSuId the primary key of the doi tuong tien su
     * @return the doi tuong tien su
     * @throws qldc.pm.tiensu.NoSuchDoiTuongTienSuException if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu findByPrimaryKey(long doiTuongTienSuId)
        throws NoSuchDoiTuongTienSuException, SystemException {
        return findByPrimaryKey((Serializable) doiTuongTienSuId);
    }

    /**
     * Returns the doi tuong tien su with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the doi tuong tien su
     * @return the doi tuong tien su, or <code>null</code> if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        DoiTuongTienSu doiTuongTienSu = (DoiTuongTienSu) EntityCacheUtil.getResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
                DoiTuongTienSuImpl.class, primaryKey);

        if (doiTuongTienSu == _nullDoiTuongTienSu) {
            return null;
        }

        if (doiTuongTienSu == null) {
            Session session = null;

            try {
                session = openSession();

                doiTuongTienSu = (DoiTuongTienSu) session.get(DoiTuongTienSuImpl.class,
                        primaryKey);

                if (doiTuongTienSu != null) {
                    cacheResult(doiTuongTienSu);
                } else {
                    EntityCacheUtil.putResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
                        DoiTuongTienSuImpl.class, primaryKey,
                        _nullDoiTuongTienSu);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(DoiTuongTienSuModelImpl.ENTITY_CACHE_ENABLED,
                    DoiTuongTienSuImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return doiTuongTienSu;
    }

    /**
     * Returns the doi tuong tien su with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param doiTuongTienSuId the primary key of the doi tuong tien su
     * @return the doi tuong tien su, or <code>null</code> if a doi tuong tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienSu fetchByPrimaryKey(long doiTuongTienSuId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) doiTuongTienSuId);
    }

    /**
     * Returns all the doi tuong tien sus.
     *
     * @return the doi tuong tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienSu> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the doi tuong tien sus.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.DoiTuongTienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of doi tuong tien sus
     * @param end the upper bound of the range of doi tuong tien sus (not inclusive)
     * @return the range of doi tuong tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienSu> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the doi tuong tien sus.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.DoiTuongTienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of doi tuong tien sus
     * @param end the upper bound of the range of doi tuong tien sus (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of doi tuong tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienSu> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<DoiTuongTienSu> list = (List<DoiTuongTienSu>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_DOITUONGTIENSU);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_DOITUONGTIENSU;

                if (pagination) {
                    sql = sql.concat(DoiTuongTienSuModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<DoiTuongTienSu>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<DoiTuongTienSu>(list);
                } else {
                    list = (List<DoiTuongTienSu>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the doi tuong tien sus from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (DoiTuongTienSu doiTuongTienSu : findAll()) {
            remove(doiTuongTienSu);
        }
    }

    /**
     * Returns the number of doi tuong tien sus.
     *
     * @return the number of doi tuong tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_DOITUONGTIENSU);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the doi tuong tien su persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tiensu.model.DoiTuongTienSu")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<DoiTuongTienSu>> listenersList = new ArrayList<ModelListener<DoiTuongTienSu>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<DoiTuongTienSu>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(DoiTuongTienSuImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
